clear all
set more off 
version 12.0
set trace off
cap log close
pause off
	
* Set path
if "`c(username)'"=="YOUR USER NAME"{
	global home "YOUR PATH TO REPLICATION FOLDER"
}
	global data "$home/Data"
	global code "$home/Code"

include "$code/1_Data Merge.do"
tempfile initial_data
save `initial_data', replace
	
/////////
// Create February Recall data wave
/////////

use `initial_data', clear

drop if missing(individual)
egen newid = group(id individual)
gen obs=1
egen count=sum(obs),by(newid)

foreach var of varlist wage_hours_feb wage_earning_feb workinghhmem_feb workself_hours_feb ///
	hhag_feb hhag_hours_feb hhent_feb hhent_hours_feb wage_feb  {
	egen `var'_max=max(`var'), by(newid)
	replace `var'=`var'_max if missing(`var')
	drop `var'_max
}

sort newid wave
bysort newid: keep if _n==1

replace wave=0
replace hoursnow=wage_hours_feb
replace earningnow=wage_earning_feb
replace agri_hrs=hhag_hours_feb
replace enter_hrs=hhent_hours_feb
replace working_l7=workinghhmem_feb 
replace workinghrs_l7=workself_hours_feb 
egen hours=rowtotal(wage_hours_feb hhag_hours_feb hhent_hours_feb)
replace hours=. if missing(hhent_hours_feb) & missing(hhag_hours_feb) & missing(wage_hours_feb)
replace workinghrs_l7=hours if missing(workinghrs_l7)
replace workinghrs_l7=hours if hours>workinghrs_l7 & !missing(hours)
drop hours
gen nolaynowork=(whoemployed_==0 & wholaidoff==0)
replace whoemployed_=.
replace whoemployed_=1 if wage_hours_feb>0 & !missing(wage_hours_feb)
replace whoemployed_=0 if wage_hours_feb==0
replace whoemployed_=1 if wage_earning_feb>0 & !missing(wage_earning_feb)
replace whoemployed_=0 if nolaynowork==1
replace whoemployed_=1 if wage_feb==1
replace sector=. if whoemployed_==0
replace working_l7=1 if whoemployed_==1
egen oldadults=rowtotal(s2_q15a_adults s2_q18_adults_gone)
replace s2_q15a_adults=oldadults

global hhctl s2_q15a_adults s2_q15c_youngchildren s2_q15b_schoolchildren s2q_17_hhmove ///
	wealth_index_norm s2_q21a_powergrid s4_q4_acres s4_q9_livestockvalue ent_lw_total ///
	head_gender head_age urban weight*
global indctl age gender ishead resp
global respctl ed_prim ed_sec ed_postsec with_spouse married

keep $hhctl $indctl $respctl strata wave hoursnow earningnow agri_hrs enter_hrs working_l7 workinghrs_l7 whoemployed_ sector ///
	individual id s2_q9a_county kids youngkids schoolkids adol* oldest* child* hhent_feb hhag_feb
tempfile wave0
save `wave0', replace

/////////
// Merge Data and Clan
/////////

use `initial_data', clear
append using `wave0'

egen uniqueindiv = group(id individual)
la var uniqueindiv "Unique Individual ID"

* Time 
gen double week=wofd(today)
format week %tw
gen double month=mofd(today)
replace month=721 if missing(month) & wave==0
format month %tm
ta month wave,mi
gen closure=inlist(wave,1,2,3)
la var closure "School closures period"

gen post=today>22199 // After Oct 12
replace post=0 if wave==0
la var post "Post"
lab def prepost 0 "Pre" 1 "Post"
lab values post prepost

* Children variables
egen young_kids=max(youngkids), by(id closure)
egen school_kids=max(schoolkids), by(id closure)
la var schoolkids "Any school-age (5-17) children"
la var youngkids "Any young (0-4) children"
la var school_kids "Any school-age (5-17) kids in 2020"
la var young_kids "Any young (0-4) kids in 2020"
la var s2_q15c_youngchildren "Count young (0-4) children"
la var s2_q15b_schoolchildren "Count school (5-17) children"
la var kids "Any children"
egen kids_count=rowtotal(s2_q15b_schoolchildren s2_q15c_youngchildren)
la var kids_count "Count of children"
lab def youngsters 0 "No child age 0-4" 1 "Any child age 0-4"
lab values young_kids youngsters
gen onlychild=kids_count==1
lab def onlykid 0 ">1 Child" 1 "1 Child"
lab values onlychild onlykid
egen only_child=max(onlychild), by(id)
lab def only_kid 0 ">1 HH child" 1 "Only 1 HH child"
lab values only_child only_kid
egen count_adolescents=max(adolescents), by(id)
la var count_adolescents "Count adolescent (10-17) children"
la var onlychild "Only 1 household child"
la var youngkids "Any below-school-age (0-4) children"

gen youngest_9up=youngest_child_age>=9
gen youngest_8down=youngest_child_age<9
lab def youngster2 0 "No child age 0-8" 1 "Any child age 0-8"
lab values youngest_8down youngster2
la var youngest_child_age "Age of youngest household child"
la var youngest_8down "Any young (0-8) children"

gen age_0_4=0
gen age_5_8=0
gen age_9_12=0
gen age_13_17=0
replace age_0_4=1 if age<5 & age>=0
replace age_5_8=1 if age<9 & age>=5
replace age_9_12=1 if age<13 & age>=9
replace age_13_17=1 if age<18 & age>=13
egen kidcount_0_4=sum(age_0_4), by(id wave)
egen kidcount_5_8=sum(age_5_8), by(id wave)
egen kidcount_9_12=sum(age_9_12), by(id wave)
egen kidcount_13_17=sum(age_13_17), by(id wave)
la var kidcount_0_4 "Count of kids ages 0-4"
la var kidcount_5_8 "Count of kids ages 5-8 (Wave 3-on)"
la var kidcount_9_12 "Count of kids ages 9-12 (Wave 3-on)"
la var kidcount_13_17 "Count of kids ages 13-17 (Wave 3-on)"
gen any_kid_5_8=kidcount_5_8>0 & !missing(kidcount_5_8)
egen anykid_5_8=max(any_kid_5_8), by(id)
la var anykid_5_8 "Any child age 5-8"
lab def kids58 0 "No child age 5-8" 1 "Any child age 5-8"
lab values anykid_5_8 kids58

* Schooling variables
replace school_level_=school_level_now_ if missing(school_level_)
gen schl2020=school_level_ if wave<5
egen school_level_2020=max(schl2020),by(id child_id)
drop schl2020
gen grade0=school_level_2020==1
gen grade1=school_level_2020==2
gen grade2=school_level_2020==3
gen grade3=school_level_2020==4
gen grade4=school_level_2020==5
gen grade5=school_level_2020==6
gen grade6=school_level_2020==7
gen grade7=school_level_2020==8
gen grade8=school_level_2020==9
gen grade9=school_level_2020==11
gen grade10=school_level_2020==12
gen grade11=school_level_2020==13
gen grade12=school_level_2020==14
foreach var of varlist grade0-grade12 {
	replace `var'=. if missing(school_level_2020)
	egen `var'_hhw=sum(`var'), by(id wave)
	replace `var'_hhw=. if wave>4
	egen `var'_hh=max(`var'_hhw), by(id)
	drop `var'_hhw
}

gen grade=.
replace grade=0 if school_level_2020==1
replace grade=1 if school_level_2020==2
replace grade=2 if school_level_2020==3
replace grade=3 if school_level_2020==4
replace grade=4 if school_level_2020==5
replace grade=5 if school_level_2020==6
replace grade=6 if school_level_2020==7
replace grade=7 if school_level_2020==8
replace grade=8 if school_level_2020==9
replace grade=9 if school_level_2020==11
replace grade=10 if school_level_2020==12
replace grade=11 if school_level_2020==13
replace grade=12 if school_level_2020==14
egen grade_max=max(grade), by(id child_id)

gen grade39=inlist(grade,3,4,5,6,7,8,9)
replace grade39=. if missing(grade)
egen grade39_count=sum(grade39),by(id wave)
egen grade_39_ct=max(grade39_count), by(id)
la var grade_39_ct "Count of kids in grades 3-9"
gen ygskl=age>4 & age<11
replace ygskl=0 if inlist(grade,3,4,5,6,7,8,9,10,11,12)
replace ygskl=. if missing(age)
egen ygskl_ct=sum(ygskl),by(id wave)
egen yg_skl_ct=max(ygskl_ct),by(id)
la var yg_skl_ct "Count of kids age 5-10 not in grades 3-9"
gen ygskl2=age>4 & age<11
replace ygskl2=. if missing(age)
egen ygskl_ct2=sum(ygskl2),by(id wave)
egen yg_skl_ct2=max(ygskl_ct2),by(id)
la var yg_skl_ct2 "Count of kids age 5-10"

gen oldskl=age>10 & age<18
replace oldskl=0 if inlist(grade,3,4,5,6,7,8,9)
replace oldskl=. if missing(age)
egen oldskl_ct=sum(oldskl),by(id wave)
egen old_skl_ct=max(oldskl_ct),by(id)
la var old_skl_ct "Count of kids age 11-17 not in grades 3-9"
gen oldskl2=age>10 & age<18
replace oldskl2=. if missing(age)
egen oldskl_ct2=sum(oldskl2),by(id wave)
egen old_skl_ct2=max(oldskl_ct2),by(id)
la var old_skl_ct2 "Count of kids age 11-17"
egen yg_kids_ct=max(kidcount_0_4), by(id)
la var yg_kids_ct "Count of kids age 0-4"
egen kids_ct=rowtotal(yg_kids_ct yg_skl_ct grade_39_ct old_skl_ct)
la var kids_ct "Count of all kids age 0-17"
su yg_kids_ct yg_skl_ct grade_39_ct old_skl_ct kids_ct kids_count
gen any_ygskl=yg_skl_ct>0
gen any_ygkid=yg_kids_ct>0
gen any_oldskl=old_skl_ct>0
la var any_ygskl "Any child age 5-10 not in grades 3-9"
la var any_oldskl "Any child age 11-17 not in grades 3-9"
la var any_ygkid "Any child age 0-4"

replace returndate=returndate_school_ if missing(returndate)
replace returndate=returndate_school_ if returndate_school_<returndate
egen return_date=min(returndate), by(id child_id)
format return_date %td

gen in_school_reopen=s2c_q4_oncereopen
replace in_school_reopen=1 if today>22199 & wave==3 & inlist(grade,4,8,12) & s2c_q4_oncereopen==2
replace in_school_reopen=0 if inlist(in_school_reopen,2,3)
replace in_school_reopen=1 if return_date>=22160 & return_date<=22280 & wave==3 & inlist(grade,4,8,12)
replace s3_q12_schoolgo=1 if return_date>=22160 & return_date<=22280  & wave==3 & inlist(grade,4,8,12)

gen trt_ctl=0 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0)
replace trt_ctl=1 if (grade4_hh>0 | grade8_hh>0)
replace trt_ctl=2 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0) & (grade4_hh>0 | grade8_hh>0)
replace trt_ctl=. if schoolkids==0
gen trt_ctl_add2=0 if (grade2_hh>0 | grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0)
replace trt_ctl_add2=1 if (grade4_hh>0 | grade8_hh>0)
replace trt_ctl_add2=2 if (grade2_hh>0 | grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0) & (grade4_hh>0 | grade8_hh>0)
replace trt_ctl_add2=. if schoolkids==0
gen trt_ctl_add2no9=0 if (grade2_hh>0 | grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0)
replace trt_ctl_add2no9=1 if (grade4_hh>0 | grade8_hh>0)
replace trt_ctl_add2no9=2 if (grade2_hh>0 | grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0) & (grade4_hh>0 | grade8_hh>0)
replace trt_ctl_add2no9=. if schoolkids==0
gen trt_ctl_no6=0 if (grade3_hh>0 | grade5_hh>0 | grade7_hh>0 | grade9_hh>0)
replace trt_ctl_no6=1 if (grade4_hh>0 | grade8_hh>0)
replace trt_ctl_no6=2 if (grade3_hh>0 | grade5_hh>0 | grade7_hh>0 | grade9_hh>0) & (grade4_hh>0 | grade8_hh>0)
replace trt_ctl_no6=. if schoolkids==0
gen trt_ctl_4adj=0 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0 | grade2_hh>0) // 
replace trt_ctl_4adj=1 if (grade4_hh>0)
replace trt_ctl_4adj=2 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0 | grade2_hh>0) & (grade4_hh>0) // | grade2_hh>0
replace trt_ctl_4adj=. if schoolkids==0
gen trt_ctl_8adj=0 if (grade9_hh>0  | grade6_hh>0  | grade7_hh>0 | grade10_hh>0) //
replace trt_ctl_8adj=1 if (grade8_hh>0)
replace trt_ctl_8adj=2 if (grade9_hh>0  | grade6_hh>0  | grade7_hh>0 | grade10_hh>0) & (grade8_hh>0) //| grade10_hh>0
replace trt_ctl_8adj=. if schoolkids==0
gen trt_ctl_12adj=0 if (grade10_hh>0 | grade11_hh>0) //grade9_hh>0 | 
replace trt_ctl_12adj=1 if (grade12_hh>0)
replace trt_ctl_12adj=2 if (grade10_hh>0 | grade11_hh>0) & (grade12_hh>0) //grade9_hh>0 | 
replace trt_ctl_12adj=. if schoolkids==0
gen trt_ctl_full=0 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0 | grade10_hh>0 | grade11_hh>0)
replace trt_ctl_full=1 if (grade4_hh>0 | grade8_hh>0 | grade12_hh>0)
replace trt_ctl_full=2 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0 | grade10_hh>0 | grade11_hh>0) & (grade4_hh>0 | grade8_hh>0 | grade12_hh>0)
replace trt_ctl_full=. if schoolkids==0
lab def ctrltrtmixed 0 "Control" 1 "Treat" 2 "Mixed"
foreach var of varlist trt_ctl trt_ctl_add2 trt_ctl_add2no9 trt_ctl_no6 trt_ctl_4adj trt_ctl_8adj trt_ctl_12adj trt_ctl_full {
	la var `var' "Treat"
	lab values `var' ctrltrtmixed
}

gen trt_ctl_cat=0 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0)
replace trt_ctl_cat=1 if grade4_hh>0
replace trt_ctl_cat=2 if grade8_hh>0
replace trt_ctl_cat=3 if (grade4_hh>0 & grade8_hh>0)
replace trt_ctl_cat=. if schoolkids==0
gen trt_ctl_cat2=0 if (grade3_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade9_hh>0  | grade10_hh>0  | grade11_hh>0)
replace trt_ctl_cat2=1 if grade4_hh>0
replace trt_ctl_cat2=2 if grade8_hh>0
replace trt_ctl_cat2=3 if grade12_hh>0
replace trt_ctl_cat2=4 if (grade4_hh>0 & grade8_hh>0) | (grade4_hh>0 & grade12_hh>0) | (grade12_hh>0 & grade8_hh>0)
replace trt_ctl_cat2=. if schoolkids==0
lab def tccat 0 "Control" 1 "Grade 4" 2 "Grade 8" 3 "Both"
lab def tccat2 0 "Control" 1 "Grade 4" 2 "Grade 8" 3 "Grade 12" 4 "Multiple"
lab val trt_ctl_cat tccat
lab val trt_ctl_cat2 tccat2
lab var trt_ctl_cat "Categorical treatment"
lab var trt_ctl_cat2 "Categorical treatment with grade 12"

ta age if !missing(trt_ctl) & age>17
ta age if !missing(trt_ctl) & age>17 & current_num_adults<=2
gen age_diff_oldest=age-oldest_child_age
ta age_diff_oldest if !missing(trt_ctl) & age>17
gen possible_parent=(age_diff_oldest>=14 & age_diff_oldest<=55)
ta age possible_parent if !missing(trt_ctl) & age>17  & current_num_adults<=2
ta age possible_parent if !missing(trt_ctl) & age>17  & current_num_adults==1
gen parent_carer=possible_parent
replace parent_carer=. if age<18
replace parent_carer=1 if current_num_adults==1 & age>17
ta age parent_carer if !missing(trt_ctl)

replace s3_q11_schooltype=2 if s3_q11_schooltype==3

gen treated_age=age if grade3==1 & grade3_hh==1 & age>(3+3)
forval a=4/9 {
	replace treated_age=age if grade`a'==1 & grade`a'_hh==1 & age>(`a'+3) & age<treated_age
}
replace treated_age=11 if treated_age==10.5
egen hh_min_treat_age=min(treated_age),by(id) 
gen hh_treat_young_sib=hh_min_treat_age>youngest_child_age
replace hh_treat_young_sib=. if missing(hh_min_treat_age)
replace hh_treat_young_sib=1 if young_kids==1
replace hh_treat_young_sib=1 if youngest_child_age<=6
replace hh_treat_young_sib=. if !(grade3_hh>0 | grade4_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade8_hh>0 | grade9_hh>0)
la var hh_treat_young_sib "Any sibling younger than treated child"
gen treated_age2=age if grade3==1 & grade3_hh==1 & age>(3+3)
forval a=4/12 {
	replace treated_age2=age if grade`a'==1 & grade`a'_hh==1 & age>(`a'+3) & age<treated_age2
}
replace treated_age2=11 if treated_age2==10.5
egen hh_min_treat_age2=min(treated_age2),by(id) 
gen hh_treat_young_sib2=hh_min_treat_age2>youngest_child_age
replace hh_treat_young_sib2=. if missing(hh_min_treat_age2)
replace hh_treat_young_sib2=1 if young_kids==1
replace hh_treat_young_sib2=1 if youngest_child_age<=6
replace hh_treat_young_sib2=. if !(grade3_hh>0 | grade4_hh>0 | grade5_hh>0 | grade6_hh>0  | grade7_hh>0 | grade8_hh>0 | grade9_hh>0 | grade10_hh>0 | grade11_hh>0 | grade12_hh>0)
la var hh_treat_young_sib2 "Any sibling younger than treated child"

gen trtgirl=inlist(grade,3,4,5,6,7,8)
replace trtgirl=0 if gender==0
replace trtgirl=. if missing(grade)
egen trt_girl=max(trtgirl),by(id)
gen trt_girlv=-1*(trt_girl-1)
la var trt_girl "Any girl in grade 3-9"
ta trt_ctl trt_girl

* Work variables
gen hhent=enter_hrs_>0
replace hhent=0 if missing(enter_hrs_)
replace hhent=1 if hhent_feb==1 & wave==0
replace hhent=0 if hhent_feb==0 & wave==0
replace hhent=. if missing(enter_hrs_) & wave==0
replace hhent=emp_enter if !missing(emp_enter)
replace hhent=1 if enter_hrs_>0 & !missing(enter_hrs_)
replace enter_hrs_=0 if hhent==0
ren enter_hrs_ enter_hrs
la var hhent "Engaged in HH non-ag enterprise in last 7 days"

gen hhag=agri_hrs>0
replace hhag=0 if missing(agri_hrs)
replace hhag=1 if hhag_feb==1 & wave==0
replace hhag=0 if hhag_feb==0 & wave==0
replace hhag=. if missing(agri_hrs_) & wave==0
replace hhag=emp_agri if !missing(emp_agri)
replace hhag=1 if agri_hrs>0 & !missing(agri_hrs)
replace agri_hrs=0 if hhag==0
ren agri_hrs_ agri_hrs
la var hhag "Engaged in HH agriculture in last 7 days"

replace whoemployed_=emp_wage if !missing(emp_wage)
replace whoemployed_=1 if hoursnow>0 & !missing(hoursnow)
replace whoemployed_=0 if missing(whoemployed_) & wave>0
replace hoursnow=0 if whoemployed_==0

replace working_l7=1 if (hhent==1 | hhag==1 | whoemployed_==1)
replace working_l7=0 if (hhent==0 & hhag==0 & whoemployed_==0)
replace working_l7=employed if !missing(employed)
replace working_l7=1 if workinghrs_l7>0 & !missing(workinghrs_l7)

replace sector=1 if hhag==1 & missing(sector)
replace sector=. if working_l7==0

bys wave: su working_l7 hhent hhag whoemployed_
bys wave: su workinghrs_l7 agri_hrs enter_hrs hoursnow

gen r0=1 if wave==0
egen r0_obs=max(r0),by(uniqueindiv)
foreach var of varlist working_l7 whoemployed_ hhag hhent {
	gen `var'0=`var' if wave==0
	egen `var'_r0=max(`var'0), by(uniqueindiv)
}
foreach var of varlist working_l7_r0 whoemployed__r0 hhag_r0 hhent_r0 {
	replace `var'=. if r0_obs==0
}
la var working_l7_r0 "Engaged in any work in February 2020"

egen work_ever=max(working_l7), by(uniqueindiv)
gen closures_any=working_l7
replace closures_any=. if !inlist(wave,1,2)
egen any_closure=max(closures_any), by(uniqueindiv)
gen closures_emp=whoemployed_
replace closures_emp=. if !inlist(wave,1,2)
egen emp_closure=max(closures_emp), by(uniqueindiv)
gen closures_ag=hhag
replace closures_ag=. if !inlist(wave,1,2)
egen ag_closure=max(closures_ag), by(uniqueindiv)
gen closures_ent=hhent
replace closures_ent=. if !inlist(wave,1,2)
egen ent_closure=max(closures_ent), by(uniqueindiv)

* Child agriculture
replace child_ag_hours=s4_q2_hours_allchild if missing(child_ag_hours)
replace child_ag_hours=s4_q2_hours_allchild if child_ag_hours<s4_q2_hours_allchild & !missing(s4_q2_hours_allchild)
egen childhrs_ag=sum(s3_hh_farm_b),by(id wave)
replace child_ag_hours=childhrs_ag if missing(child_ag_hours) & childhrs_ag!=0
replace child_ag_hours=childhrs_ag if childhrs_ag>child_ag_hours & !missing(childhrs_ag)
drop childhrs_ag
gen childag=child_ag_hours>0
replace childag=0 if missing(child_ag_hours)
gen childag0=childag
replace childag0=. if post==1
egen childag_0=max(childag0), by(id)
egen ag_tot=sum(agri_hrs), by(id wave)
replace ag_tot=ag_tot+ child_ag_hours
gen child_ag_share=child_ag_hours/ag_tot
la var childag "Any child engaged in household farm labor"
la var childag_0 "Any child engaged in household farm labor before reopening"
lab def child_hh_ag 0 "No child ag before reopening" 1 "Any child ag before reopening"
lab val childag_0 child_hh_ag
la var s4_q2_hours_allchild "Total child agricultural hours"
la var child_ag_hours "Total child agricultural hours"

* HH variables
egen ag_hh=max(hhag), by(id wave)
replace ag_hh=1 if (s4_q1_agactivity_1==1 | s4_q1_agactivity_2==1)
replace ag_hh=1 if s4_q4_acres>0 & !missing(s4_q4_acres)
replace ag_hh=0 if missing(ag_hh)
egen ent_hh=max(hhent), by(id wave)
replace ent_hh=1 if (ent_lw_total>0 & !missing(ent_lw_total))
replace ent_hh=0 if missing(ent_hh)
egen wage_hh=max(whoemployed_), by(id wave)
egen wage_earnings=sum(earningnow), by(id wave)
replace s4_q10_agearnings=0 if missing(s4_q10_agearnings)
la var wage_earnings "HH wage earnings, last 14 days"
la var s4_q10_agearnings "HH agriculture earnings, last 14 days"
la var ent_earnings "HH enterprise earnings in last 14 days"
la var ent_profits "HH enterprise profits in last 14 days"

replace hhent0=ent_hh if wave==0
egen ent_hh0=max(hhent0),by(id)
replace hhag0=ag_hh if wave==0
egen ag_hh0=max(hhag0), by(id)
gen hhag3=ag_hh if wave==3
egen ag_hh3=max(hhag3), by(id)
egen ent_ever=max(hhent), by(id)
replace ent_ever=1 if ent_hh==1
egen ag_ever=max(hhag), by(id)
replace ag_ever=1 if ag_hh==1
gen urban0=urban if wave==0
egen urban_0=max(urban0), by(id)
gen wealth_high=wealth_index_norm>0
gen wealth0=wealth_high if wave==0
egen wealth_0=max(wealth0), by(id)
gen wealth_00=1-wealth_0
gen big_urban=0
replace big_urban=1 if urban_0==1 & inlist(s2_q9a_county,1,22,32, 42,47) //Mombasa, Kiambu,Nakuru, Kisumu, Nairobi
replace big_urban=. if urban_0==1 & !inlist(s2_q9a_county,1,22,32, 42,47)

gen extraadults=current_num_adults >2
lab def manyadults 0 "1-2 Adults" 1 "3+ Adults"
lab values extraadults manyadults
la var extraadults "More than 2 household adults"
replace extraadults=. if wave==0
egen extra_adults=max(extraadults), by(id closure)
gen elderly=age>64 & !missing(age)
egen anyelder=max(elderly), by(id wave)

la var wealth_index_norm "Household wealth index"
la var ag_hh "Household engaged in agriculture"
la var ent_hh "Household engaged in enterprise"
la var ed_prim "Completed primary school"
la var ed_sec "Completed secondary school"
la var ed_postsec "Completed school beyond secondary"
la var married "Married"
la var current_num_adults "Count adults"
la var head_gender "Female household head"
la var s2_q21a_powergrid "Connected to electricity grid"

* Baseline work
foreach v of varlist working_l7 whoemployed_ hhag hhent workinghrs_l7 hoursnow agri_hrs enter_hrs {
	gen `v'_0=`v' if wave==0
	egen `v'_0m=max(`v'_0), by(id individual)
	gen `v'_0missing=missing(`v'_0m)
	egen `v'_0mi=max(`v'_0missing), by(id individual)
	replace `v'_0m=. if `v'_0mi==1
	drop `v'_0mi `v'_0missing `v'_0
}

* Trim work and childcare variables
local trim workinghrs_l7 childcare_hr hoursnow agri_hrs_ enter_hrs_ wage_earnings s4_q10_agearnings ent_earnings ent_profits
foreach v of varlist `trim' {
	su `v',d
	replace `v' = `r(p99)' if `v'>`r(p99)' & !missing(`v')
}
*replace s4_q2_hours_allchild =0 if missing(s4_q2_hours_allchild) 
replace child_ag_hours =0 if missing(child_ag_hours) 
replace child_ag_hours=s3_hh_farm_b if s3_hh_farm_b>child_ag_hours & !missing(s3_hh_farm_b)
replace childcare_hr=140 if childcare_hr>140 & !missing(childcare_hr)
egen hh_earnings=rowtotal(wage_earnings s4_q10_agearnings ent_earnings)
la var hh_earnings "HH total earnings, last 14 days"
su `trim'

replace workinghrs_l7=agri_hrs if agri_hrs>workinghrs_l7 & !missing(agri_hrs)
gen nonagri_hrs=workinghrs_l7-agri_hrs
la var nonagri_hrs "Work hours outside HH ag, last 7 days"

* Treatment definitions
replace month=800 if month==729 & post==1 // split October into two FEs, use 800 as placeholder value
gen posttreat=post*trt_ctl
lab def posttreatval 0 "Control" 1 "Post x Treat" 2 "Post x Mixed"
lab val posttreat posttreatval
lab var posttreat "Post-reopening x Treatment"
gen posttreatcat=post*trt_ctl_cat
lab def posttreatcatval 0 "Control" 1 "Post x Grade 4" 2 "Post x Grade 8" 3 "Post x Both"
lab val posttreatcat posttreatcatval
lab var posttreatcat "Post-reopening x Treatment grades"
replace trt_ctl=. if grade12_hh>0
replace posttreat=. if grade12_hh>0
gen posttreat_full=post*trt_ctl_full

* Final Prep
gen monum=month
replace monum=729 if monum==800 // had split October into two FEs, used 800 as placeholder value
ta monum
gen t=monum+post
replace t=t-724
replace t=t+1 if today>22141 & month==727
recode t 0=1 2=3 4=5 6=7 -2=0 10=9 11=9
ta t month if !missing(trt_ctl)
lab def tmo 0 "Feb" 1 "May-Jun" 3 "Jul-Aug 15" 5 "Aug 16-Oct 11" 7 "Oct 12-Nov" 9 "Jan-Mar"
la val t tmo

ta ag_hh if missing(s4_q2_hours_allchild) & !missing(trt_ctl) & inlist(wave,1,2,3,4) // 100% non-ag
ta s4_q2_hours_allchild if ag_hh==1 & !missing(trt_ctl) & resp==1 & inlist(wave,1,2,3,4),mi // none missing
replace s4_q2_hours_allchild=0 if missing(s4_q2_hours_allchild) // all non-ag HHs
egen agever=max(ag_hh),by(id)
replace s4_q3_outsideworker=0 if ag_hh==1 & missing(s4_q3_outsideworker)
gen hired_ag=s4_q3_outsideworker>0
replace hired_ag=. if missing(s4_q3_outsideworker)
replace hired_ag=0 if ag_hh==1 & missing(hired_ag)
gen anychildlabor=s4_q2_hours_allchild>0
replace anychildlabor=. if missing(s4_q2_hours_allchild)
egen anychildag=max(anychildlabor),by(id)
la var anychildag "Any child ag labor"
gen anychildlaborpre=anychildlabor
replace anychildlaborpre=. if wave>1
egen anychildagpre=max(anychildlaborpre),by(id)
egen adultaghrs=sum(agri_hrs),by(id wave)
gen oadultaghrs=adultaghrs-agri_hrs
egen adultworkhrs=sum(workinghrs_l7),by(id wave)
gen oadultwkhrs=adultworkhrs-workinghrs_l7
gen oadultnonaghrs=oadultwkhrs-oadultaghrs
egen hh_care_hrs=sum(childcare_hr),by(id wave)
su hh_care_hrs if wave==3 & resp==1,d
replace hh_care_hrs=r(p99) if hh_care_hrs>r(p99) & wave>=3 & resp==1 & !missing(hh_care_hrs)
gen areaplanted=s4_q4_acres if inlist(wave,1,2,3)
egen area_acres=max(areaplanted), by(id)
ttest area_acres if resp==1 & wave==3 & !missing(trt_ctl), by(wealth_0) unequal // not significant
replace only_child=0 if trt_ctl==2
egen childcare_hr_precl=max(childcare_hr_pre),by(id individual)
replace childcare_hr=childcare_hr_precl if !missing(childcare_hr_precl) & wave==0
gen respgender=gender if resp==1
egen resp_fem=max(respgender),by(id wave)
la var urban "Urban"
la var workinghrs_l7 "Total work hours in last 7 days"
la var hoursnow "Wage employment hours in last 7 days"
la var agri_hrs "HH agriculture hours in last 7 days"
la var wealth_0 "Above mean household wealth"
la var enter_hrs "HH non-ag enterprise hours in last 7 days"
gen anygrade8=(grade7_hh>0 | grade8_hh>0 | grade9_hh>0)
replace anygrade8=. if missing(grade4_hh)
la var ag_hh "Any HH agriculture activity"
la var ent_hh "Any HH non-ag enterprise activity"
la var s4_q2_hours_allchild "Total child HH farm labor hours in last 7 days"
gen kidcount=kids_count
replace kidcount=4 if kidcount>4
lab def countkid 0 "0" 1 "1" 2 "2" 3 "3" 4 "4+"
lab values kidcount countkid

save "$data/indivs_merge.dta", replace
